/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.kernel;

import org.openaecfm.model.geometry.RepresentationMap;
import org.openaecfm.model.kernel.TypeObject;
import org.openaecfm.model.kernel.TypeProduct;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.StringParameter;

public final class TypeProductIO implements
		IEntityInstanceUtilInput<TypeProduct>,
		IEntityInstanceUtilOutput<TypeProduct> {
	private IEntityInstanceUtilInput<TypeObject> utilInput;
	private IEntityInstanceUtilOutput<TypeObject> utilOutput;

	public void setUtilOutput(IEntityInstanceUtilOutput<TypeObject> utilOutput) {
		this.utilOutput = utilOutput;
	}

	public void setUtilInput(IEntityInstanceUtilInput<TypeObject> utilInput) {
		this.utilInput = utilInput;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void exportSPF(TypeProduct pojo, EntityInstance ei,
			IAppDataManager appDataManager) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set RepresentationMaps
		if (pojo.getRepresentationMaps().isEmpty()) {
			DollarParameter d = DollarParameter.getInstance();
			sei.addParameter(d);
		} else {
			ListParameter l = new ListParameter();
			for (RepresentationMap rm : pojo.getRepresentationMaps()) {
				EntityInstanceNameParameter n = new EntityInstanceNameParameter(
						appDataManager.getName(rm));
				l.add(n);
			}
			sei.addParameter(l);
		}

		// Set Tag
		if (pojo.getTag() == null) {
			DollarParameter d = DollarParameter.getInstance();
			sei.addParameter(d);
		} else {
			StringParameter s = new StringParameter(pojo.getTag(),
					StringParameter.UTF_8);
			sei.addParameter(s);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveNotReferenceParameter(EntityInstance ei, TypeProduct pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set Tag
		Parameter tag = sei.getParameter(7);
		if (tag.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) tag;
			pojo.setTag(s.toUTF8());
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei, TypeProduct pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set RepresentationMaps
		Parameter repre = sei.getParameter(6);
		if (repre.getNodeType() == NodeType.LIST) {
			ListParameter l = (ListParameter) repre;
			for (Parameter p : l) {
				EntityInstanceNameParameter n = (EntityInstanceNameParameter) p;
				pojo.getRepresentationMaps().add(
						(RepresentationMap) appDataManager.getObject(n
								.toInstanceName()));
			}
		}
	}
}
